import { constantRoutes, asyncRoutes } from '@/router'
const state = {
  // 记录初始路由
  routes: constantRoutes
}
const mutations = {
  // 添加动态路由
  setRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  filterRotes(context, menus) {
    // 遍历出拥有权限的路由
    const newRoutes = asyncRoutes.filter(item => menus.some(key => item.name === key))
    context.commit('setRoutes', newRoutes)
    return newRoutes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
